package com.dragonfly.shutter.services;

import java.util.Date;

import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.CollectionDAO;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PagedBean;

/**
 * 收藏列表服务类
 * @author 黄挺
 */
public class CollectionListService {
	private CollectionDAO collectionDAO;
	private BrowseLogDAO browseLogDAO;
	private static int pageSize = 10;
	
	/**
	 * 收藏列表服务函数
	 * @param userID 用户ID
	 * @param page 分页页码
	 * @param requestURL 请求URl
	 * @return 分页Bean
	 */
	public PagedBean doService(Integer userID, Integer page, String requestURL) {
		BrowseLog browseLog = new BrowseLog(userID, new Date(System.currentTimeMillis()), requestURL);
		
		PagedBean pagedBean = new PagedBean();
		pagedBean.setPageNum(page);
		pagedBean.setPageSize(pageSize);
		
		return doDBOperate(userID, page, pagedBean, browseLog);
	}
	
	/**
	 * 收藏列表数据库操作函数
	 * @param userID 用户ID
	 * @param page 分页页码
	 * @param pagedBean 分页Bean
	 * @param browseLog 日志Bean
	 * @return 分页Bean
	 */
	public PagedBean doDBOperate(Integer userID, Integer page, PagedBean pagedBean, BrowseLog browseLog){
		int minRow = ( page - 1 ) * pageSize + 1;
		int maxRow = page * pageSize; 
		
		pagedBean.setPagedItem(collectionDAO.selectPagedCollectionByUserId(userID, minRow, maxRow));
		pagedBean.setItemNum(collectionDAO.selectCollectionCount(userID));
		int maxPage = (int) java.lang.Math.ceil( (double)pagedBean.getItemNum() / (double)pageSize );
		pagedBean.setMaxPage(maxPage);
		browseLogDAO.insertBrowseLog(browseLog);
		
		return pagedBean;
	}

	public void setCollectionDAO(CollectionDAO collectionDAO) {
		this.collectionDAO = collectionDAO;
	}

	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}
}
